package net.pws.common.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtils {
    
    /**
     * 创建缩略图 <br>
     * 如果全不指定宽度和高度，则生成与原图大小一样的，<br>
     * 如果只指定宽度或高度，则根据原图按比例缩放
     * 
     * @param file
     *            源图片文件
     * @param snapshotPath
     *            缩略图存放路径
     * @param width
     *            缩略图宽度，可不指定
     * @param height
     *            缩略图高度，可不指定
     * @throws IOException 
     */
    public static void createSnapshot(File file,
                                      String snapshotPath,
                                      int width,
                                      int height) throws IOException {
        if (width <= 0 && height <= 0) {
            IoUtil.copy(file, snapshotPath);
            return;
        }
        
        BufferedImage srcImage = ImageIO.read(file);
        int sw = srcImage.getWidth();
        int sh = srcImage.getHeight();
        
        // 按比例缩放
        if (width <= 0) {
            width = (int) (((float) height / sh) * sw);
        }
        if (height <= 0) {
            height = (int) (((float) width / sw) * sh);
        }
        createSnapshort(srcImage, snapshotPath, width, height);
    }
    
    public static void createSnapshot(File file,
                                      String snapshotPath,
                                      int width,
                                      int height,
                                      boolean maxLimited) throws IOException {
        if (!maxLimited) {
            createSnapshot(file, snapshotPath, width, height);
            return;
        }
        
        if (width <= 0 || height <= 0) {
            createSnapshot(file, snapshotPath, width, height);
            return;
        }
        
        BufferedImage srcImage = ImageIO.read(file);
        int sw = srcImage.getWidth();
        int sh = srcImage.getHeight();
        
        float wScale = (float) width / sw;
        float hScale = (float) height / sh;
        float scale = wScale < hScale ? wScale : hScale;
        width = (int) (sw * scale);
        height = (int) (sh * scale);
        createSnapshort(srcImage, snapshotPath, width, height);
    }
    
    private static void createSnapshort(BufferedImage srcImage,
                                        String snapshotPath,
                                        int width,
                                        int height) throws IOException {
        BufferedImage tag = new BufferedImage(width,
                                              height,
                                              BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(srcImage, 0, 0, width, height, null); // 绘制缩小后的图
        File snapFile = new File(snapshotPath);
        ImageIO.write(tag, "JPEG", snapFile);
    }
    
    public static void main(String[] s) throws IOException {
        File srcFile = new File("C:/Users/LiangBin/Desktop/test/1.jpg");
        String snapPath = "C:/Users/LiangBin/Desktop/test/1_m.jpg";
        createSnapshot(srcFile, snapPath, 600, 400, true);
    }
}
